package com.zg.common.core.dao.scf.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.dao.scf.entity.Credit;
import com.zg.common.core.dao.scf.entity.PortPlan;
import com.zg.common.core.dao.scf.entity.Product;
import com.zg.common.core.req.PageReq;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ProductMapper extends BaseMapper<Product> {

    @Select("<script>" +
            "SELECT sp.* ," +
            "sc.credit_name AS creditName, " +
            "cmp.id AS company_id, " +
            "cmp.company_name AS bankName, " +
            "spt.product_type AS productType  " +
            "FROM scf_product sp " +
            "LEFT JOIN  scf_credit sc on sc.id = sp.credit_id " +
            "LEFT JOIN scf_product_type spt on sc.product_type_id =spt.id " +
            "LEFT JOIN scf_company cmp on cmp.id = sc.bank_id and cmp.company_type='2' " +
            "<where>" +
            "  sc.is_del = 0 " +
            "   <if test=\"pageReq.productName != null and pageReq.productName!= ''\"> AND sp.product_name LIKE '%' || #{pageReq.productName} || '%'</if>" +
            "   <if test=\"pageReq.state != null and pageReq.state!= ''\"> AND sp.state=#{pageReq.state} </if>" +
            "   <if test=\"pageReq.creditName != null and pageReq.creditName!= ''\"> AND sc.credit_name LIKE '%' || #{pageReq.creditName} || '%'</if>" +
            "   <if test=\"pageReq.productType != null and pageReq.productType!= ''\"> AND spt.product_type LIKE '%' || #{pageReq.productType} || '%'</if>" +
            "   <if test=\"pageReq.creditType != null and pageReq.creditType!= ''\"> AND sc.credit_type =#{pageReq.creditType}</if>" +
            "   <if test=\"pageReq.creditProcess != null and pageReq.creditProcess!= ''\"> AND sc.credit_process LIKE '%' || #{pageReq.creditProcess} || '%'</if>" +
            "   <if test=\"pageReq.bankName != null and pageReq.bankName!= ''\"> AND cmp.company_name LIKE  '%' || #{pageReq.bankName} || '%' </if>" +
            "</where>" +
            "</script>")
    Page<Product> page(Page<Product> page,
                      @Param("pageReq") PageReq pageReq);
}
